import 'dart:async';

import 'package:enjoy_plus_hm/utils/eventbus.dart';
import 'package:enjoy_plus_hm/utils/http.dart';
import 'package:enjoy_plus_hm/utils/token.dart';
import 'package:enjoy_plus_hm/utils/totast.dart';
import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
//1.获取短信验证码倒计时
  int _countdown = 60;
  bool _isCountDown = false;
  Timer? _timer;
  void _startCountDown() {
    //1.1 判断是否已经倒计时
    if (_isCountDown) {
      return;
    }
    //1.2 设置开始倒计时
    _isCountDown = true;
    //1.3 开始倒计时
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        if (_countdown > 0) {
          _countdown--;
        } else {
          _isCountDown = false;
          _countdown = 60;
          _timer?.cancel();
        }
      });
    });
  }

  //2.创建两个控制器获取输入框的值
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _codeController = TextEditingController();

  //3.获取短信验证码
  Future<void> _getCode() async {
    //3.1 获取输入框的值
    String mobile = _phoneController.text;
    //3.2对手机号校验(非空，正则)
    if (mobile.isEmpty) {
      return ToastUtil.showError('请输入手机号');
    }
    RegExp regExp = RegExp(r'^1[3-9]\d{9}$');
    if (!regExp.hasMatch(mobile)) {
      return ToastUtil.showError('请输入正确的手机号');
    }
    //3.3开始倒计时
    _startCountDown();

    //3.4调用验证码接口
    var res = await http.get('/code', params: {
      'mobile': mobile,
    });
    // print(res);
    if (res['code'] != 10000) return ToastUtil.showError('获取验证码失败');
    ToastUtil.showSuccess('验证码发送成功');

    //延迟执行
    Future.delayed(const Duration(seconds: 2), () {
      // 把验证码回显到输入框中
      _codeController.text = res['data']['code'];
    });
  }

//4.登录
  void _login() async {
    String mobile = _phoneController.text;

//4.1校验手机号
    if (mobile.isEmpty) {
      return ToastUtil.showError('请输入手机号');
    }
    RegExp regExp = RegExp(r'^1[3-9]\d{9}$');
    if (!regExp.hasMatch(mobile)) {
      return ToastUtil.showError('请输入正确的手机号');
    }
//4.2校验验证码
    if (_codeController.text.isEmpty) {
      return ToastUtil.showError('请输入验证码');
    }
    if (_codeController.text.length != 6) {
      return ToastUtil.showError('请输入6位验证码');
    }
    RegExp regExp2 = RegExp(r'^\d{6}$');
    if (!regExp2.hasMatch(_codeController.text)) {
      return ToastUtil.showError('请输入6位数字验证码');
    }
//4.3调用登录接口
    var res = await http.post('/login', data: {
      'mobile': mobile,
      'code': _codeController.text,
    });
    if (res['code'] != 10000) {
      return ToastUtil.showError('登录失败');
    }
//4.4存储token
    await TokenManager().saveToken(res['data']['token']);
//4.4清除定时器
    _timer?.cancel();

    eventBus.fire(RefreshMineEvent());
    //4.6返回上一页
    Navigator.pop(context);
  }

  @override
  void dispose() {
    _phoneController.dispose();
    _codeController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: const Text('登录'),
          backgroundColor: Colors.transparent,
          centerTitle: true,
        ),
        body: Container(
            padding: const EdgeInsets.all(20),
            child:
                Column(mainAxisAlignment: MainAxisAlignment.start, children: [
              const Row(
                children: [
                  Text('登录',
                      style:
                          TextStyle(fontSize: 25, fontWeight: FontWeight.bold))
                ],
              ),
              const SizedBox(height: 10),
              const Row(
                children: [
                  Text('加入享+, 让生活更轻松', style: TextStyle(fontSize: 15))
                ],
              ),
              const SizedBox(height: 30),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: TextField(
                      controller: _phoneController,
                      keyboardType: TextInputType.phone,
                      maxLength: 11,
                      style: const TextStyle(fontSize: 16),
                      decoration: InputDecoration(
                        labelText: '手机号',
                        hintText: '请输入手机号',
                      ),
                    ),
                  ),
                  ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.white,
                      shadowColor: Colors.transparent,
                      foregroundColor: const Color.fromARGB(255, 85, 145, 175),
                      minimumSize: const Size(100, 50),
                    ),
                    onPressed: () {
                      _getCode();
                    },
                    child: _isCountDown
                        ? Text(
                            '$_countdown s后重新获取',
                            style: const TextStyle(color: Colors.grey),
                          )
                        : const Text('获取验证码'),
                  ),
                ],
              ),
              const SizedBox(height: 8),
              TextField(
                controller: _codeController,
                keyboardType: TextInputType.number,
                maxLength: 6,
                style: TextStyle(fontSize: 16),
                decoration: InputDecoration(
                  labelText: '验证码',
                  hintText: '请输入6位验证码',
                ),
              ),
              const SizedBox(height: 8),
              const Row(children: [
                Text('未注册手机号经验证后将自动登录',
                    style: TextStyle(fontSize: 12, color: Colors.grey)),
              ]),
              const SizedBox(height: 50),
              Row(children: [
                Expanded(
                    child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color.fromARGB(255, 85, 145, 175),
                    minimumSize: const Size(100, 50),
                  ),
                  onPressed: () {
                    _login();
                  },
                  child: const Text('登录',
                      style: TextStyle(color: Colors.white, fontSize: 20)),
                ))
              ])
            ])));
  }
}
